﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-09-26 15:52:36
 * 功能：单个城市的UI管理
**************************************************************************************/

using Game.Framework;
using UnityEngine;
using UnityEngine.UI;

namespace Game.UI.HallPanel
{
    public class CityItem : MonoBehaviour
    {
        private Button m_LockBtn, m_UnlockBtn;
        private GameObject m_Lock, m_Character;
        private Text m_Label;
        private GameDataModel.City m_Data;

        private void Find()
        {
            m_LockBtn = transform.Find("LockBtn").GetComponent<Button>();
            m_UnlockBtn = transform.Find("UnLockBtn").GetComponent<Button>();
            m_Lock = transform.Find("Lock").gameObject;
            m_Character = transform.Find("Character").gameObject;
            m_Label = transform.Find("Lable").GetComponent<Text>();

            // 监听按钮
            m_LockBtn.onClick.AddListener(ClickCityBtn);
            m_UnlockBtn.onClick.AddListener(ClickCityBtn);

            // 监听事件
            EventCenter.Instance.AddEventListen<string>(GameEventName.OnCityUnlock, OnCityUnlock);
        }

        private void OnCityUnlock(string cityName)
        {
            if (m_Data.name != cityName)
            {
                m_Character.SetActive(false);
                return;
            }
            // 刷新UI
            Init(m_Data);
        }

        public void ClickCityBtn()
        {
            UIUtility.DisplayPanel(CityPanel.CityPanelView.PANEL_KEY, "UI/CityPanel/Prefabs/CityPanel", data: m_Data);
        }

        public void Init(GameDataModel.City data)
        {
            m_Data = data;
            if (m_LockBtn == null) Find();
            name = data.name;
            m_LockBtn.gameObject.SetActive(!data.isUnlocked);
            m_UnlockBtn.gameObject.SetActive(data.isUnlocked);
            m_Lock.SetActive(!data.isUnlocked);
            m_Character.SetActive(data.isUnlocked && GameDataMgr.Instance.MapInfo.currentCountry == data.countryName && GameDataMgr.Instance.MapInfo.currentCity == data.name);
            m_Label.text = data.name;
        }

        private void OnDestroy()
        {
            EventCenter.Instance.RemoveEventListen<string>(GameEventName.OnCityUnlock, OnCityUnlock);
        }
    }
}